/*
 *  generator.h
 *  
 *
 *  Created by Alexandar Kozarev on 3/22/11.
 *  Copyright 2011 The Neurosciences Institute, La Jolla, CA. All rights reserved.
 *
 */

#ifndef _RAND_GEN_
#define _RAND_GEN_

#include <iostream>

/*
 Uniform random number generator.
 Returns a floating-point number in the range [0,1).
 */

const unsigned int jflone = 0x3f800000;
const unsigned int jflmsk = 0x007fffff;

class UnifRandGen
{
public:
	UnifRandGen(unsigned int seed = 0)
	{
		state = seed;
	}; // end: UnifRandGen

	float Urand01(void)
	{
		unsigned int temp;
		state = 1664525 * state + 1013904223;
		temp = jflone | (jflmsk & state);
		return((*(float *)&temp) - 1.0);
	}; // end: rand

	int UrandI(int min, int max)
	{
		int range = max - min;
		return ( (int) ( Urand01() * (float)range ) ) + min; 
	}; // end: rand
	
	void srand(unsigned int seed)
	{
		state = seed;
	}; // end: srand
	
private:
	unsigned int state;
}; // end: class UnifRandGen

#endif // end: _RAND_GEN_
